package com.kinyx.framework.db.daos;

import java.util.List;

import com.kinyx.framework.db.sentence.defines.SelectSentence;
import com.kinyx.framework.enums.impl.ErrorCode1;
import com.kinyx.framework.exception.KRuntimeException;
import com.kinyx.framework.vo.InPageData;
import com.kinyx.framework.vo.OutPageData;

public interface Dao extends _Dao_SQL, _Dao_Sentence_get, _Dao_HQL_find, _Dao_HQL_change, _Dao_HQL_batchChange {

	/**
	 * 查询单个值
	 * @param <V> 泛型
	 * @param dp 数据访问参数
	 * @param select 查询语句对象
	 * @param clazz 返回值类型
	 * @return 单个值
	 */
	@SuppressWarnings("deprecation")
	@Override
	default <V> V getValue(final DaoParam dp, final SelectSentence select, final Class<V> clazz) {
		if (!select.isSQL()) { throw new KRuntimeException(ErrorCode1.CODE_ERROR_803); }
		return this.queryValue(dp, select.getSentence(), select.getParams(), clazz);
	}

	/**
	 * 查询列表数据
	 * @param <B> 泛型
	 * @param dp 数据访问参数
	 * @param select 查询语句对象
	 * @param clazz 返回值类型
	 * @return 列表数据
	 */
	@SuppressWarnings("deprecation")
	@Override
	default <B> List<B> getList(final DaoParam dp, final SelectSentence select, final Class<B> clazz) {
		return select.isSQL() ? this.queryList(dp, select.getSentence(), select.getParams(), clazz, select.getPageSize()) : this.findList(dp, select.getSentence(), select.getParams(), clazz, select.getPageSize());
	}

	/**
	 * 查询单个数据
	 * @param <B> 泛型
	 * @param dp 数据访问参数
	 * @param select 查询语句对象
	 * @param clazz 返回值类型
	 * @return 单个数据
	 */
	@SuppressWarnings("deprecation")
	@Override
	default <B> B getOne(final DaoParam dp, final SelectSentence select, final Class<B> clazz) {
		return select.isSQL() ? this.queryOne(dp, select.getSentence(), select.getParams(), clazz) : this.findOne(dp, select.getSentence(), select.getParams(), clazz);
	}

	/**
	 * 查询分页数据
	 * @param <B> 泛型
	 * @param dp 数据访问参数
	 * @param select 查询语句对象
	 * @param clazz 返回值类型
	 * @return 分页数据
	 */
	@SuppressWarnings("deprecation")
	@Override
	default <B> OutPageData<B> getPage(final DaoParam dp, final SelectSentence select, final Class<B> clazz) {
		return select.isSQL() ? this.queryPage(dp, select.getSentence(), select.getParams(), clazz, new InPageData().pageNumber(select.getPageNumber()).pageSize(select.getPageSize())) : this.findPage(dp, select.getSentence(), select.getParams(), clazz, new InPageData().pageNumber(select.getPageNumber()).pageSize(select.getPageSize()));
	}

}
